package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.bo.ReportBo;
import com.wande.dataplatform.domain.dto.ChartDataDTO;
import com.wande.dataplatform.domain.vo.ReportVO;
import com.wande.dataplatform.service.IReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 报表设计控制器
 *
 * @author wande
 */
@Validated
@RestController
@RequestMapping("/dataplatform/report")
@RequiredArgsConstructor
public class ReportController extends BaseController {

    private final IReportService reportService;

    /**
     * 查询报表设计列表
     */
    @SaCheckPermission("dataplatform:report:list")
    @GetMapping("/list")
    public TableDataInfo<ReportVO> list(ReportBo bo, PageQuery pageQuery) {
        return reportService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询报表设计详情
     */
    @SaCheckPermission("dataplatform:report:query")
    @GetMapping("/{id}")
    public R<ReportVO> getInfo(@PathVariable Long id) {
        return R.ok(reportService.queryById(id));
    }

    /**
     * 新增报表设计
     */
    @SaCheckPermission("dataplatform:report:add")
    @Log(title = "报表设计", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody ReportBo bo) {
        return toAjax(reportService.insertByBo(bo));
    }

    /**
     * 修改报表设计
     */
    @SaCheckPermission("dataplatform:report:edit")
    @Log(title = "报表设计", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody ReportBo bo) {
        return toAjax(reportService.updateByBo(bo));
    }

    /**
     * 删除报表设计
     */
    @SaCheckPermission("dataplatform:report:remove")
    @Log(title = "报表设计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(reportService.deleteByIds(Arrays.asList(ids)));
    }

    /**
     * 获取图表数据
     */
    @SaCheckPermission("dataplatform:report:query")
    @PostMapping("/{reportId}/chart/{chartId}/data")
    public R<ChartDataDTO> getChartData(
            @PathVariable Long reportId,
            @PathVariable String chartId,
            @RequestBody(required = false) Map<String, Object> filters) {
        return R.ok(reportService.getChartData(reportId, chartId, filters));
    }

    /**
     * 导出报表
     */
    @SaCheckPermission("dataplatform:report:export")
    @Log(title = "报表设计", businessType = BusinessType.EXPORT)
    @GetMapping("/{reportId}/export")
    public R<byte[]> exportReport(
            @PathVariable Long reportId,
            @RequestParam(defaultValue = "PDF") String format) {
        byte[] data = reportService.exportReport(reportId, format);
        return R.ok(data);
    }

    /**
     * 发布报表
     */
    @SaCheckPermission("dataplatform:report:publish")
    @Log(title = "报表设计", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/publish")
    public R<Void> publish(@PathVariable Long id) {
        return toAjax(reportService.publishReport(id));
    }

    /**
     * 下线报表
     */
    @SaCheckPermission("dataplatform:report:offline")
    @Log(title = "报表设计", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/offline")
    public R<Void> offline(@PathVariable Long id) {
        return toAjax(reportService.offlineReport(id));
    }
}
